package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.ProjectDO;
import com.example.makerspace.dto.PageResult;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.dto.Result;
import com.example.makerspace.dto.project.ProjectDTO;
import com.example.makerspace.mapper.ProjectMapper;
import com.example.makerspace.service.ProjectService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.Date;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public Result create(ProjectDTO projectDTO) {
        ProjectDO projectDO = new ProjectDO();
        projectDO.setProjectName(projectDTO.getProjectName());
        projectDO.setLanguageId(projectDTO.getLanguageId());
        projectDO.setTypeId(projectDTO.getTypeId());
        projectDO.setProjectDescribe(projectDTO.getProjectDescribe());
        projectDO.setUrl(projectDO.getUrl());
        projectDO.setIsPrivate(projectDTO.getIsPrivate());
        projectMapper.insert(projectDO);
        return Result.success(true);
    }

    @Override
    public Result query(ProjectDTO projectDTO) {
        QueryWrapper<ProjectDO> queryWrapper = new QueryWrapper<>();
        if (projectDTO.getLanguageId() != null) {
            queryWrapper.eq("language_id", projectDTO.getLanguageId());
        }
        if (projectDTO.getTypeId() != null) {
            queryWrapper.eq("type_id", projectDTO.getTypeId());
        }
        if (!StringUtils.isEmpty(projectDTO.getProjectName())) {
            queryWrapper.like("project_name", projectDTO.getProjectName());
        }
        Page<ProjectDO> page = new Page<>(projectDTO.getPagination().getPageNum(), projectDTO.getPagination().getSize());
        Page<ProjectDO> projectDOPage = projectMapper.selectPage(page, queryWrapper);
        PageResult pageResult = PageResult.success();
        Pagination pagination = Pagination.builder()
                .size(page.getSize())
                .pageNum(projectDTO.getPagination().getSize())
                .total(projectDOPage.getTotal())
                .totalPages(projectDOPage.getPages())
                .build();
        pageResult.setPagination(pagination);
        pageResult.setObj(projectDOPage.getRecords());
        return pageResult;
    }

    @Override
    public Result delete(Integer id) {
        projectMapper.deleteById(id);
        return Result.success(true);
    }

    @Override
    public Result update(ProjectDTO projectDTO) {
        ProjectDO projectDO = new ProjectDO();
        projectDO.setId(projectDTO.getId());
        projectDO.setProjectName(projectDTO.getProjectName());
        projectDO.setLanguageId(projectDTO.getLanguageId());
        projectDO.setTypeId(projectDTO.getTypeId());
        projectDO.setProjectDescribe(projectDTO.getProjectDescribe());
        projectDO.setUrl(projectDTO.getUrl());
        projectDO.setIsPrivate(projectDTO.getIsPrivate());
        projectDO.setModifyTime(new Date());
        projectMapper.updateById(projectDO);
        return Result.success(true);
    }
}
